#!/opt/lampp/bin/perl -w
use strict;
use warnings;
use DBI;

require('function.pl');

load_proxies();

my $db_conn = DBI->connect("DBI:mysql:database=phone_num_search;host=127.0.0.1", 'root', '');
$db_conn->do('set names utf8');
my $sql = 'select prefix from prefix order by tag desc';
my $request = $db_conn->prepare($sql);
my $result = $request->execute();
while (my ($prefix) = $request->fetchrow_array) {
	next if ($prefix % $ARGV[0] != $ARGV[1]);
	my $start_phone_num = execute_scalar("select max(phone_num) from result where phone_num between '${prefix}0000' and '${prefix}9999'", $db_conn);
	$start_phone_num = substr($start_phone_num, 7);
	print "start with $start_phone_num\n";
	for (my $i = $start_phone_num + 1; $i < 10000; ++$i) {
		my $suffix = sprintf('%04d', $i);
		my $phone_num = "$prefix$suffix";
#		next if (execute_scalar("select count(*) from result where phone_num = '$phone_num'", $db_conn) > 0);
		my $url = 'http://wap.baidu.com/ssid=0/from=0/bd_page_type=1/uid=DA5D563F8CAE6908956BED67B0ADCC6B/s?word='.$phone_num.'&uc_param_str=upssntdnvelami&st_1=111041&st_2=102041&pu=sz%40224_220&idx=20000&tn_1=webmain&tn_2=fwapadv&ct_1=%E6%90%9C%E7%BD%91%E9%A1%B5';
		my $content = fetch_url_via_proxy($url);
		my @results;
		my @items = split('<div class="resitem" >', $content);
		foreach my $item (@items) {
			my ($title, $site);
			$title = $1 if ($item =~ /\d+&#160;([\d\D]+?)<\/a>/);
			$site = $1 if ($item =~ /<span class="site">(.+?)<\/span>/);
			next if (!defined($title) || !defined($site));
			next if (index($site, 'baidu.com') > 0);
			push @results, [$site, $title];
#			print "$site\t$title\n";
		}
		my $rank = 1;
		foreach my $result (@results) {
			my $url = $result->[0];
			my $title = $db_conn->quote($result->[1]);
			my $sql = "replace into result(prefix, phone_num, rank, url, title) values('$prefix', '$phone_num', $rank, '$url', $title)";
			$db_conn->do($sql);
			last if (++$rank > 5);
		}
		print "$phone_num\t".(scalar @results)."\n";
	}
}
